package com.semp.gu.core.local;

import java.io.File;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.semp.gu.domain.local.LocalAlbumElement;
import com.semp.gu.domain.local.LocalAlbumElement.AlbumElementType;

public class LocalAlbumElementConverter implements ILocalAlbumElementConverter {

	private List<String>	imageExtList;
	private List<String>	videoExtList;
	
	private final static char EXT_SEPARATOR = '.';
	private final static Logger logger = LoggerFactory.getLogger(LocalAlbumElementConverter.class);


	public LocalAlbumElement fileToLocalAlbumElement(File file) {
		if (file == null || file.isDirectory())
			return null;

		LocalAlbumElement newLocalAlbumElement = new LocalAlbumElement(file);
		newLocalAlbumElement.setName(file.getName());
		newLocalAlbumElement.setType(getType(newLocalAlbumElement.getName()));
		newLocalAlbumElement.setDate(new Date(file.lastModified()));
		
		return newLocalAlbumElement;
	}

	/**
	 * Get AlbumElementType given file name (based on file extension)
	 * @param fileName
	 * @return
	 */
	private AlbumElementType getType(String fileName) {
		String fileExtension = fileName.substring(fileName.lastIndexOf(EXT_SEPARATOR)+ 1).toLowerCase();
		AlbumElementType type = AlbumElementType.UNKNOWN;
		
		if (imageExtList.contains(fileExtension)) {
			type = AlbumElementType.PICTURE;
		} else if (videoExtList.contains(fileExtension)) {
			type = AlbumElementType.MOVIE;
		}
		logger.debug("{} typed as {}", fileName, type);
		return type;
	}

	public List<String> getImageExtList() {
		return imageExtList;
	}

	public void setImageExtList(List<String> imageExtList) {
		this.imageExtList = imageExtList;
	}

	public List<String> getVideoExtList() {
		return videoExtList;
	}

	public void setVideoExtList(List<String> videoExtList) {
		this.videoExtList = videoExtList;
	}

}
